import 'package:common_utils/common_utils.dart';
import 'package:flustars/flustars.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:nft_shop/my/model/models.dart';
import 'package:nft_shop/net/dio_api.dart';
import 'package:nft_shop/net/service_repository.dart';
import 'package:nft_shop/res/constant.dart';
import 'package:nft_shop/res/global.dart';
import 'package:nft_shop/util/log_utils.dart';

import '../model/models.dart';

class MarketDetailsConteroller extends GetxController {
  static final marketDetailsInfo= "MarketDetailsInfo";
  static final ContractKey= "contractKey";
  String _countDownTime = "";

  late GoodsInfo _goodsinfo;
  ///状态
  late int status = LoadStatusType.loading;
  //1单个藏品详情 2整体铸造详情  3整体详情
  int type = 0;
  //合约地址
  String contract = "";

  ///基本信息高度
  double heght1 = 175;
  ///创作者介绍高度
  double heght2 = 175;
  ///作品描述高度
  double heght3 = 175;
  ///当前高度
  double currentHeight = 0;


  GoodsInfo get goodsinfo => _goodsinfo;

  //是否存在
  bool get hasGoodsinfo => ObjectUtil.isNotEmpty(_goodsinfo);

  String get countDownTime => _countDownTime;

  set setCountDownTime(value) {
    _countDownTime = value;
    update(['countDownTime']);
  }

  static MarketDetailsConteroller get to => Get.put(MarketDetailsConteroller());

  @override
  void onInit() {
    // TODO: implement onInit
    super.onInit();
    getConfigNameDetail();
  }

  @override
  void onReady() {
    // TODO: implement onReady
    super.onReady();
  }

  @override
  void onClose() {
    // TODO: implement onClose
    super.onClose();
  }

  //获取数据
  void getDataList(String id) async {
    try {
      //1单个藏品详情 2整体铸造详情  3整体详情
      if(type == 1){
        _goodsinfo = await ServiceRepository.goodsInfo(id:id);
      }else if(type == 2){
        _goodsinfo = await ServiceRepository.collectionInfo(id:id);
      }else if(type == 3){
        _goodsinfo = await ServiceRepository.specialFirstGoods(id:id);
      }
      status = LoadStatusType.success;
    } catch (e, s) {
      status = LoadStatusType.empty;
      http.setError(e, s);
    }
    update();
  }

  void getConfigNameDetail()async{
    if(ObjectUtil.isNotEmpty(SpUtil.getString(ContractKey))){
      contract = SpUtil.getString(ContractKey).toString();
    }else{
      try{
        List list = await ServiceRepository.getConfigNameDetail("contract_url");
        list.forEach((element) {
          if(element.name == "contract_url"){
            if(ObjectUtil.isNotEmpty(element)){
              ConfigNameDetail detail = element;
              contract = detail.value??"";
              SpUtil.putString(ContractKey, contract);
            }
          }
        });
      }catch(e,s){
        status = LoadStatusType.empty;
      }
    }
    update();
  }

  //计算高度
  void getPageHeight(){
    currentHeight = heght1;

    heght2 = Global.boundingTextSize(_goodsinfo.info!.introduce??"",TextStyle(fontSize: 12,color: Global.kTheme.inputHint1,fontFamily: Global.mySemiboldFont),maxWidth:  ScreenUtil.getScreenW(Get.context!) -30).height + 120;

    heght3 = Global.boundingTextSize(_goodsinfo.info!.detail??"",TextStyle(fontSize: 12,color: Global.kTheme.inputHint1,fontFamily: Global.mySemiboldFont),maxWidth:  ScreenUtil.getScreenW(Get.context!) -30).height + 70;
  }

  ///改变高度
  void changeHeight(int index){
    if(index == 0){
      currentHeight = heght1;
    }else if(index == 1){
      currentHeight = heght2;
    }else if(index == 2){
      currentHeight = heght3;
    }
    update([marketDetailsInfo]);
  }


}
